#!/home/y/bin/perl

use FindBin qw/$Bin/;
use lib qw(lib ../lib);
use Test::More qw(no_plan);
use Farsail::Util qw(:all);
use Farsail;
use Log::Log4perl qw/:easy/;
Log::Log4perl->easy_init($DEBUG);

$ENV{FARSAILDIR} ||= "$Bin/../conf";
use DBI;
use_ok("Farsail::Bulkload");

my $farsail = Farsail->instance();
my $config = $farsail->config;
$farsail->init();

my $test_data = $config->get_expand_file(
    'rootdir',
    '../data/authors.tsv',
);

if ( 0 ) {
    $farsail->dispatch(
        'bulkload', 
        file => $test_data,
        format => 'tsv',
        database => 'demo',
        table => 'authors',
    );
    ok(1, "bulkload file");
}

if ( 0 ) {
    $farsail->dispatch(
        'bulkload', 
        file => $test_data,
        format => 'tsv',
        database => 'demo',
        truncate => 1,
        table => 'authors',
    );
    ok(1, "bulkload file with truncate option");
}

if ( 0 ) {
    $farsail->dispatch(
        'bulkload', 
        file => $test_data,
        format => 'tsv',
        database => 'demo2',
        table => 'authors',
        create_sql => <<'SQL',
drop table if exists $table;
create table $table (
  id int,
  first_name text,
  last_name text
);
SQL
    );
    ok(1, "bulkload file with create_sql option and remote db");
}

if ( 0 ) {
    $farsail->dispatch(
        'bulkload', 
        file => $test_data,
        format => 'tsv',
        cluster => 'farsail',
        table => 'authors',
        create_sql => <<'SQL',
drop table if exists $table;
create table $table (
  id int,
  first_name text,
  last_name text
);
SQL
    );
    ok(1, "bulkload file to cluster");
}

if ( 0 ) {
    $farsail->dispatch(
        'bulkload', 
        dir => $config->get_expand_file('rootdir', '../data'),
        prefix => 'bucket_',
        format => 'tsv',
        cluster => 'farsail',
        table => 'authors',
        truncate => 1,
    );
    ok(1, "bulkload file to cluster");
}

if ( 1 ) {
    $farsail->dispatch(
        'bulkload', 
        dir => $config->get_expand_file('rootdir', '../data'),
        prefix => 'bucket_',
        format => 'tsv',
        cluster => 'farsail',
        table => 'authors',
        create_sql => <<'SQL',
drop table if exists $table;
create table $table (
  id int,
  first_name text,
  last_name text
);
SQL
    );
    ok($farsail->config->get('bulkload.done_file'), "bulkload file to cluster");
}
